<?php
/**
 * @version      BOSSCMS-Free
 * @trademark    BOSSCMS
 * @copyright    Copyright © by BOSSCMS-Free (https://www.bosscms.net/) 保留所有权利
 * @license      https://gitee.com/Greenpeas/BOSSCMS/license/ 和 https://www.bosscms.net/freelicense/
 * @link         https://www.bosscms.net/
 * @author       温州软界网络技术有限公司
**/
defined('IS_OK') or exit('Access Forbidden');

into::basic_class('admin');

class link extends admin
{
	public function init()
	{
		global $G;
		$G['cover'] = $this->cover();
		$data = page::link_pages($G['rows']=$this->rows(20));
		echo $this->theme('link/link', $data);
	}

	public function exchange()
	{
		global $G;
		$this->cover('link','R');
		if(is_numeric($G['get']['id'])){
			$res = mysql::select_one('link','link',"id='{$G['get']['id']}'");
			$host = parse_url($G['config']['domain'],PHP_URL_HOST);
			$json = json::get($file=ROOT_PATH.'cache/json/link_exchange.json');
			$md5 = md5($host.'-'.$res['link']);
			if($json[$md5] && $json[$md5]['time']<TIME+3600){
				$json[$md5]['id'] = $G['get']['id'];
				echo json::encode($json[$md5]);
				die();
			}else{
				into::basic_class('curl');
				preg_match("/<a[^>]+?{$host}[^>]+?>/i",curl::request($res['link']),$match);
				$data = array('id'=>$G['get']['id'],'time'=>TIME);
				if($match[0]){
					$data['msg'] = '成功';
					$data['state'] = 'success';
					$data['color'] = 'green';
					if(preg_match('/rel=([\'|"])nofollow\\1/',$match[0])){
						$data['msg'] .= '<small color="red">(nofollow)</small>';
					}
				}else{
					$data['msg'] = '失败';
					$data['state'] = 'error';
					$data['color'] = 'red';
				}
				echo json::encode($data);
				unset($data['id']);
				$json[$md5] = $data;
				json::put($file,$json);
				die();
			}
		}
	}
	
	public function modify()
	{
		global $G;
		$this->cover('link','M');
		if(isset($G['post']['id'])){
			$bosscms_error=array();
			foreach($G['post']['id'] as $id){
				if(isset($G['post']['sort'.$id])){
					$data = array('sort'=>$G['post']['sort'.$id]);
				}else if(isset($G['post']['display'.$id])){
					$data = array('display'=>$G['post']['display'.$id]);
				}
				if($data && !is_numeric(mysql::update($data,"link","id='{$id}'"))){
					$bosscms_error[]=$id;
				}
			}
			if($bosscms_error){
				alert('ID为'.implode(',',$bosscms_error).'修改失败');
			}else{
				alert('修改成功！', url::mpf('link','link','init'));
			}
		}else{
			alert('没有提交信息！');
		}
	}
	
	public function edit()
	{
		global $G;
		/* boss_CMS */
		$G['cover'] = $this->cover('link');
		$data = array();
		if(isset($G['get']['id'])){
			$data = mysql::select_one('*','link',"id='{$G['get']['id']}'");
		}
		$data['subarr'] = page::items_option(0,false,array(),true);
		echo $this->theme('link/edit', $data);
	}
	
	public function add()
	{	
		global $G;
		$this->cover('link',$G['get']['id']?'M':'A');
		if(isset($G['post'])){
			$data = array(
				'name'        => $G['post']['name'],
				'items'       => $G['post']['items']?$G['post']['items']:'',
				'type'        => $G['post']['type'],
				'image'       => $G['post']['image'],
				'link'        => $G['post']['link'],
				'nofollow'    => $G['post']['nofollow'],
				'target'      => $G['post']['target'],
				'sort'        => $G['post']['sort'],
				'display'     => $G['post']['display']
			);
			if(($id=$G['get']['id'])){
				if($result = mysql::select_one('id','link',"id='{$id}'")){
					mysql::update($data,"link","id='{$id}'");
				}else{
					alert('没有内容');
				}
			}else{
				$id = mysql::insert($data,"link");
			}
			alert('操作成功', url::mpf('link','link','edit',array('id'=>$id,'success'=>'ok')));
		}else{
			alert('没有提交信息');
		}
	}
	
	public function delete()
	{
		global $G;
		$this->cover('link','D');
		if(isset($G['post']['url']) && isset($G['get']['id'])){
			$del = array();
			$arr = explode(',',$G['get']['id']);
			foreach($arr as $id){
				if(is_numeric($id)){
					$del[$id] = $id;
				}
			}
			if($del){
				$error=array();
				foreach($del as $id=>$name){
					if(is_numeric(mysql::delete("link","id='{$id}'"))){
						
					}else{
						$error[]=$id;
					}
				}
				if($error){
					alert('ID为'.implode(',',$error).'删除失败');
				}else{
					alert('删除成功');
				}
			}else{
				$_booscms;
				alert('没有删除对象id');
			}			
		}
		alert('没有提交信息');
	}
}
?>